package com.ztjava.common.controller;

import java.io.File;

import com.jfinal.aop.Duang;
import com.xiaoleilu.hutool.io.FileUtil;
import com.ztjava.common.Common;
import com.ztjava.common.model.files.FilesInfo;
import com.ztjava.common.service.FilesService;
import com.ztjava.common.tool.files.QiniuTool;
import com.ztjava.core.Constant;
import com.ztjava.core.annotation.ControllerClass;
import com.ztjava.core.client.PortalClient;
import com.ztjava.ext.auth.annotation.UserController;
import com.ztjava.ext.auth.annotation.UserMethod;

@ControllerClass(path="/common/files",client=PortalClient.class)
@UserController(portal=Constant.P_PORTAL)
public class FilesPortalController extends FilesBaseController{
	private static FilesService filesService = Duang.duang(FilesService.class);
	
	/***
	 * 上传文件
	 */
	@UserMethod
	public void upload(){
		keepPara();
		render("upload.html");
	}
	
	/*************
	 *Info列表
	**************/
	@UserMethod("CommonFilesInfo")
	public void infoList(){
		
		if(!getPara("is_ajax","F").equals("T")){
			keepPara();
			render("infoList.html");
			return;
		}
		renderPageJson(filesService.getInfoPage(client, getParaMap()));
		
	}

	@UserMethod("CommonFilesInfo")
	public void deleteInfo(){
		Integer id = getParaToInt("id");
		FilesInfo filesInfo = FilesInfo.dao.findById(id);
		if(filesInfo!=null){
			FileUtil.del(new File(filesInfo.getStr("foler")));
			if(filesInfo.getStr("type").equals(Common.qiniu)){
				QiniuTool.deleteFile(filesInfo.getStr("path"));
			}
			filesInfo.delete();
			renderSuccess();
		}else {
			renderError("文件不存在！");
		}
	}
}
